package io.gitee.hefren.hhcache.conf;

import jakarta.annotation.Resource;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * @Date 2024/6/30
 * @Author lifei
 */
@Component
public class HhApplicationListener implements ApplicationListener<ApplicationEvent> {

    @Resource
    private List<HhPlugin> hhPluginList;

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if (event instanceof ApplicationReadyEvent) {
            // ApplicationRunner
            if (Objects.nonNull(hhPluginList)) {
                for (HhPlugin hhPlugin : hhPluginList) {
                    hhPlugin.init();
                    hhPlugin.startup();
                }
            }
        } else if (event instanceof ContextClosedEvent) {
            // PreDestroy
            if (Objects.nonNull(hhPluginList)) {
                for (HhPlugin hhPlugin : hhPluginList) {
                    hhPlugin.shutdown();
                }
            }
        }
    }
}
